import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_dk_form/flutter_dk_form.dart';

class SingleInputDemoPage extends StatelessWidget {
  static const String routeName = '/page/single_input_demo';
  static const String name = "SingleInputDemoPage";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('SingleInputDemoPage'),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            SingleInputWidget(
              title: '单行输入',
              hintText: '我是提示',
            ),
            SingleInputWidget(
              title: '带初始值',
              initialValue: '我是初始值',
              hintText: '我是提示',
            ),
            SingleInputWidget(
              title: '不带下划线',
              hintText: '我是提示',
              hasLine: false,
            ),
            SingleInputWidget(
              title: '必填',
              hintText: '我是提示',
              allowEmpty: false,
            ),
            SingleInputWidget(
              title: '数字输入',
              hintText: '我是提示',
              inputCharFormatterType: InputCharFormatterType.InputCharFormatterDigital,
            ),
            SingleInputWidget(
              title: '小数输入',
              hintText: '我是提示',
              inputCharFormatterType: InputCharFormatterType.InputCharFormatterDecimal,
            ),
            SingleInputWidget(
              title: '我有单位',
              hintText: '我是提示',
              unitText: 'm²',
            ),
            _InputPage(),
          ],
        ),
      ),
    );
  }
}


class _InputPage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() => _InputState();
}

class _InputState extends State<_InputPage>{
  String init = 'abc';
  String input;
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        SingleInputWidget(
          title: '单行输入',
          hintText: '我是提示',
          initialValue: init,
          onChanged: onChanged,
        ),
        Text(input??init),
        RaisedButton(
          child: Text('重置'),
          onPressed: reset,
        )
      ],
    );
  }

  void onChanged(value){
    setState(() {
      input = value;
    });
  }

  void reset(){
    setState(() {
      init = 'abcd';
    });
  }
}